package com.sq.partner.manager.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.IGatewayMenuDao;
import com.sq.partner.manager.mybatis.model.GatewayMenu;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

@Repository
public class GatewayMenuDaoImpl extends BaseDaoImpl implements IGatewayMenuDao {

	private static Logger logger = LogUtil.getLog(BaseDaoImpl.class);
	
	@Override
	public Page queryPage(Page page, Integer PageIndex, GatewayMenu menu)
			throws Exception {
		Connection conn = null;
		//查总条数
		PreparedStatement pstmt = null;
		//查分页
		PreparedStatement pstmt1 = null;
		ResultSet rs = null;
		List<String> list=new ArrayList<String>();
		Page p = null;
		try{
			conn = this.getConnection();
			StringBuffer sqlbuf=new StringBuffer();
			sqlbuf.append(
//					"select distinct m.menuid,m.parent_menu_id,m.menu_name,nvl(p.menu_name,'无') parent_menu_name,m.url,m.menu_desc,m.sort_no,m.oper_time from t_gateway_menu m left join t_gateway_menu p on m.parent_menu_id = p.menuid connect by m.menuid = prior m.parent_menu_id start with m.menuid in (select menuid from t_gateway_menu)"
					"select distinct m.menuid,m.parent_menu_id,m.menu_name,nvl(p.menu_name,'无') parent_menu_name,m.url,m.menu_desc,m.sort_no,m.oper_time,(case m.menu_type when '0' then '只开放给总公司' else '开放给分公司' end) menu_type_desc,m.menu_type from t_gateway_menu m left join t_gateway_menu p on m.parent_menu_id = p.menuid where 1=1"
					);
			String sql;
			if(null!=menu.getMenuName()&&StrUtil.isNotEmpty(menu.getMenuName())){
				sqlbuf.append(" and m.menu_name like ? ");
				list.add("%"+menu.getMenuName()+"%");
			}
			sqlbuf.append(" connect by m.menuid = prior m.parent_menu_id start with m.menuid in (m.menuid) order by m.oper_time desc");
			sql=sqlbuf.toString();
			logger.debug("执行SQL：" + sql);


			pstmt = conn.prepareStatement("select count(*) from ("+sql+")");
			for (int i = 0; i < list.size(); i++) {
				pstmt.setObject(i+1, list.get(i));
			}
			rs = pstmt.executeQuery();
			rs.next();
			Long total = rs.getLong(1);

			//查出分页
			p = new Page(page.getCurrent(),total);
			SqlUtil sqlUtil=new	SqlUtil();
			String pagesql=sqlUtil.buildPageSqlForOracle(sql,p).toString();
			logger.debug("执行SQL："+pagesql);
			pstmt1 = conn.prepareStatement(pagesql);
			for (int i = 0; i < list.size(); i++) {
				pstmt1.setObject(i+1, list.get(i));
			}
			rs = pstmt1.executeQuery();
			List lists = DBUtil.transRs2List(rs);
			p.setData(lists);
		}finally{
			close(rs);
			close(pstmt);
			close(conn);
		}
		return p;
	}

	@Override
	public List<GatewayMenu> getAllMenus(long userid) throws Exception {
		List<GatewayMenu> list = this.queryList("queryAllMenus",userid);
		return list;
	}

	@Override
	public boolean addMenu(GatewayMenu menu) throws Exception {
		return this.add("insertGatewayMenu", menu);
	}

	@Override
	public boolean modifyMenu(GatewayMenu menu) throws Exception {
		return this.update("updateGatewayMenuNotNull", menu);
	}

	@Override
	public List<GatewayMenu> getAllMenusExceptSelf(Long menuid)
			throws Exception {
		List<GatewayMenu> list = this.queryList("queryAllMenusExceptSelf",menuid);
		return list;
	}

	@Override
	public boolean deleteMenu(Long menuid) throws Exception {
		return this.delete("delGatewayMenuById", menuid);
	}

	@Override
	public List<GatewayMenu> queryChildMenuIsExists(Long parentMenuid)
			throws Exception {
		List<GatewayMenu> list = this.queryList("queryChildMenu", parentMenuid);
		return list;
	}

	@Override
	public List<GatewayMenu> queryAllMenuIsAsParentMenu(Map<String,Object> map)
			throws Exception {
		List<GatewayMenu> list = this.queryList("queryGatewayMenuByMap", map);
		return list;
	}


	@Override
	public GatewayMenu queryMenuInfo(Long menuid) throws Exception {
		Object obj = this.query("queryGatewayMenuInfoById", menuid);
		if(null!=obj){
			GatewayMenu menu = (GatewayMenu)obj;
			return menu;
		}
		return null;
	}
}
